package com.pug.zixun.controller.state;

import com.alibaba.fastjson.JSONObject;
import com.pug.zixun.commons.anno.IgnoreToken;
import com.pug.zixun.controller.BaseController;
import com.pug.zixun.mapper.StateMapper;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.Path;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022-08-11$ 21:57$
 */
@RestController
@RequiredArgsConstructor
public class StateController extends BaseController {

    private final StateMapper stateMapper;


    /**
     * 统计订单和用户相关
     *
     * @return
     */
    @PostMapping("/state/orderuser")
    public Map<String, Object> countIndexData() {
        return stateMapper.stateIndexData();
    }


    // 建立一个线程池对象
    ExecutorService executorService = Executors.newCachedThreadPool();

    /**
     * 统计首页相关所有的信息
     *
     * @return
     */
    @PostMapping("/state/indexdata")
    public JSONObject stateIndexData() throws InterruptedException {
        // 闭锁 （栅栏）
        CountDownLatch countDownLatch = new CountDownLatch(4);
        // 返回数据
        JSONObject jsonObject = new JSONObject();
        // 线程1
        executorService.submit(() -> {//100ms
            //1 : 统计操作系统
            List<Map<String, Object>> osLists = stateMapper.stateOS(null);
            jsonObject.put("osLists", osLists);
            countDownLatch.countDown();
        });
        // 线程2
        executorService.submit(() -> {//200ms
            //2 : 统计浏览器
            List<Map<String, Object>> browerLists = stateMapper.stateRequestBrower(null);
            jsonObject.put("broswerLists", browerLists);
            countDownLatch.countDown();
        });
        // 线程3
        executorService.submit(() -> {//150ms
            //3 : 统计性别
            List<Map<String, Object>> userSexLists = stateMapper.stateUserSex(null);
            jsonObject.put("userSexLists", userSexLists);
            countDownLatch.countDown();
        });

        // 线程4
        executorService.submit(() -> {//150ms
            //3 : 统计性别
            List<Map<String, Object>> orderLists = stateMapper.stateOrderRelation(null);
            jsonObject.put("orderLists", orderLists);
            countDownLatch.countDown();
        });

        // 拦截--- 这里是一堵墙壁 ，这里一定是转化成0才会放开
        countDownLatch.await();

        return jsonObject;// 空结果
    }

    /**
     * 根据时间条件查询订单统计信息
         * 0: 全部
         * 1: 今天
         * 2: 昨天
         * 3：近三天
         * 4：近一周
         * 5：上个月
         * 6：本月
     * @param type
     * @return
     */
    @PostMapping("/state/order/{type}/{ctype}")
    public  List<Map<String, Object>> stateOrderByTime(@PathVariable("type") Integer type,
                                                       @PathVariable("ctype") Integer ctype
                                                       ) {
        String condition = null;
        if (type.equals(1)) { // 今天
            condition = "TO_DAYS(create_time) = TO_DAYS(now())";
        } else if (type.equals(2)) { // 昨天
            condition = "TO_DAYS(NOW())-TO_DAYS(create_time) <= 1";
        } else if (type.equals(3)) { // 近三天
            condition = "DATE_SUB(NOW(), INTERVAL 3 DAY) <= date(create_time)";
        } else if (type.equals(4)) { // 近一周
            condition = "DATE_SUB(NOW(), INTERVAL 7 DAY) <= date(create_time)";
        } else if (type.equals(5)) { // 上个月
            condition = "PERIOD_DIFF(DATE_FORMAT( now(),'%Y%m'),DATE_FORMAT(create_time, '%Y%m' )) = 1";
        } else if (type.equals(6)) { // 本月
            condition = "DATE_FORMAT(create_time, '%Y%m' ) = DATE_FORMAT( now(),'%Y%m')";
        }

         List<Map<String, Object>> mapList = null;
        if(ctype.equals(1)) { // 根据时间统计订单
            mapList = stateMapper.stateOrderRelation(condition);
        }else if(ctype.equals(2)) { // 根据时间统计操作系统
            mapList = stateMapper.stateOS(condition);
        }else if(ctype.equals(3)) { // 根据时间统计浏览器
            mapList = stateMapper.stateRequestBrower(condition);
        }else if(ctype.equals(4)) { // 根据时间统计用户性别
            mapList = stateMapper.stateUserSex(condition);
        }
        return  mapList;
    }


//    /**
//     * 统计首页相关所有的信息
//     *
//     * @return
//     */
//    @PostMapping("/state/indexdata")
//    public JSONObject stateIndexData() throws InterruptedException, ExecutionException, TimeoutException {
//        // 线程1
//        Future<List<Map<String, Object>>> future1 = executorService.submit(() -> {
//            //1 : 统计操作系统
//            List<Map<String, Object>> osLists = stateMapper.stateOS();
//            return osLists;
//        });
//
//        // 线程2
//        Future<List<Map<String, Object>>> future2 =executorService.submit(()->{
//            //2 : 统计浏览器
//            List<Map<String, Object>> browerLists = stateMapper.stateRequestBrower();
//            return browerLists;
//        });
//
//
//        // 线程3
//        Future<List<Map<String, Object>>> future3 = executorService.submit(() -> {
//            //3 : 统计性别
//            List<Map<String, Object>> userSexLists = stateMapper.stateUserSex();
//            return userSexLists;
//        });
//
//        // 这里建议给时间，稍微给长
//        List<Map<String, Object>> maps1 = future1.get(5L,TimeUnit.SECONDS);
//        List<Map<String, Object>> maps2= future2.get(5L,TimeUnit.SECONDS);
//        List<Map<String, Object>> maps3 = future3.get(5L,TimeUnit.SECONDS);
//
//        // 返回数据
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("osLists",maps1);
//        jsonObject.put("browerLists",maps2);
//        jsonObject.put("userSexLists",maps3);
//
//        return jsonObject;// 空结果
//    }

}
